<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:f="livcos.org/ns/xslt/function"
  exclude-result-prefixes="c f xs">

  <xsl:param name="livcos_version" select="'0.2.2'"/>
  
	<xsl:param name="viewObjId"/>
	<xsl:param name="viewRootId"/>
	<xsl:param name="projRootId"/>
	<xsl:param name="relativeName"/>
	
	<xsl:param name="toRootPath"/>
	<xsl:param name="toDomainPath"/>
	<xsl:param name="toProjPath"/>
	<xsl:param name="toViewPath"/>

	<xsl:variable name="jobObject" select="/*"/>
  
	<xsl:function name="f:encode-for-tagname">
		<xsl:param name="val"/>
		<xsl:variable name="tmp"
			select="translate(replace(encode-for-uri(translate($val, ' ', '_')), '~', '_E7'), '%', '_')"/>
		<xsl:value-of select="if (matches($tmp, '^[a-zA-Z_].*')) then $tmp else concat('_', $tmp)"/>
	</xsl:function>
  
  <!-- Cosmos functions -->
  
	<xsl:function name="f:object-name" as="xs:string">
		<xsl:param name="obj"/>
		<xsl:value-of select="tokenize(f:object-relative-name($obj), '/')[last()]"/>
	</xsl:function>
	
	<xsl:function name="f:object-relative-name" as="xs:string">
		<xsl:param name="node"/>
		<xsl:variable name="absId" select="($node/ancestor-or-self::*/@id[starts-with(., '/')])[last()]"/>
		<xsl:choose>
			<xsl:when test="contains($absId, '#')">
				<xsl:value-of select="substring-before($absId, '#')"/>
			</xsl:when>
			<xsl:when test="$absId">
				<xsl:value-of select="$absId"/>
			</xsl:when>
			<xsl:when test="$node/ancestor-or-self::c:*">
				<xsl:value-of select="string-join(for $c in $node/ancestor-or-self::c:* return local-name($c), '/')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="tokenize(base-uri($node), '/')[last()]"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
	
	<xsl:function name="f:object-id" as="xs:string">
    <xsl:param name="node"/>
    <xsl:variable name="relativeName" select="f:object-relative-name($node)"/>
    <xsl:choose>
      <xsl:when test="starts-with($relativeName, '/')">
        <xsl:value-of select="$relativeName"/>
      </xsl:when>
      <xsl:otherwise>
      	<xsl:variable name="sub" select="substring-after($relativeName, '/')"/>
				<xsl:value-of
					select="concat(substring-after(base-uri($node), 'cosmos:'), if ($sub != '') then '/' else '', $sub)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>
  
  <xsl:function name="f:cosmos-uri" exclude-result-prefixes="#all">
    <xsl:param name="str"/>
    <xsl:param name="base"/>
    <uri>
    	<xsl:variable name="_uri" select="f:uri($str)"/>
    	<xsl:variable name="_base" select="f:object-id($base)"/>
    	<obj>
    		<xsl:choose>
    			<xsl:when test="$_uri/path">
    				<xsl:value-of select="resolve-uri($_uri/path, $_base)"/>
    			</xsl:when>
    			<xsl:otherwise>
    				<xsl:value-of select="$_base"/>
    			</xsl:otherwise>
    		</xsl:choose>
    	</obj>
      <xsl:if test="$_uri/fragment">
      	<id><xsl:value-of select="$_uri/fragment"/></id>
      </xsl:if>
    </uri>
  </xsl:function>
  
	<xsl:function name="f:resolve-object-id" as="xs:string">
		<xsl:param name="objId"/>
		<xsl:param name="base"/>
		<xsl:value-of select="resolve-uri($objId, f:object-id($base))"/>
	</xsl:function>
	
	<!-- extension functions -->
  
  <xsl:function name="f:format-duration">
    <xsl:param name="duration"/>
    <xsl:if test="hours-from-duration($duration) &gt; 0">
      <xsl:value-of select="hours-from-duration($duration)"/>
      <xsl:text>:</xsl:text>
    </xsl:if>
    <xsl:if test="minutes-from-duration($duration) &gt; 0">
      <xsl:value-of select="minutes-from-duration($duration)"/>
      <xsl:text>:</xsl:text>
    </xsl:if>
    <xsl:value-of select="seconds-from-duration($duration)"/>
  </xsl:function>
  
  <xsl:function name="f:total-seconds">
    <xsl:param name="duration"/>
    <xsl:value-of
      select="hours-from-duration($duration) * 3600 + minutes-from-duration($duration) * 60 + seconds-from-duration($duration)"
    />
  </xsl:function>
  
	<xsl:function name="f:isValidUrl" as="xs:boolean">
		<xsl:param name="str"/>
		<xsl:value-of select="not(contains($str, '\'))"/>
	</xsl:function>
	
	<xsl:function name="f:uri" xmlns:uri="java:java.net.URI" exclude-result-prefixes="#all">
		<xsl:param name="str"/>
		<xsl:variable name="uri" select="uri:new($str)"/>
		<xsl:variable name="_tmp">
			<scheme><xsl:value-of select="uri:getScheme($uri)"/></scheme>
			<user-info><xsl:value-of select="uri:getUserInfo($uri)"/></user-info>
			<host><xsl:value-of select="uri:getHost($uri)"/></host>
			<port><xsl:value-of select="uri:getPort($uri)"/></port>
			<path><xsl:value-of select="uri:getPath($uri)"/></path>
			<query><xsl:value-of select="uri:getQuery($uri)"/></query>
			<fragment><xsl:value-of select="uri:getFragment($uri)"/></fragment>
		</xsl:variable>
		<uri>
			<xsl:for-each select="$_tmp/*[(. != '') and (. != '-1')]">
				<xsl:copy-of select="."/>
			</xsl:for-each>
		</uri>
	</xsl:function>
	
</xsl:stylesheet>
